package dao;

import model.Ticket;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 车票数据访问对象
 */
public class TicketDao {
    public static boolean addTicket(Ticket ticket) {
        String sql = "INSERT INTO tickets (用户ID, 列车ID, 座位号, 订票时间, 票价, 是否已取消, 是否学生票) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?)";
        try (Connection conn = BaseDao.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, ticket.getUserId());
            pstmt.setInt(2, ticket.getTrainId());
            pstmt.setString(3, ticket.getSeatNumber());
            pstmt.setTimestamp(4, new Timestamp(ticket.getBookingTime().getTime()));
            pstmt.setDouble(5, ticket.getPrice());
            pstmt.setBoolean(6, ticket.isCancelled());
            pstmt.setBoolean(7, ticket.isStudentTicket());
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 根据用户ID获取车票
    public List<Ticket> getByUserId(int userId) {
        List<Ticket> tickets = new ArrayList<>();
        String sql = "SELECT * FROM tickets WHERE 用户ID = ? AND 是否已取消 = false";
        try (Connection conn = BaseDao.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, userId);
            try (ResultSet rs = pstmt.executeQuery()) {
                while (rs.next()) {
                    tickets.add(mapTicket(rs));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return tickets;
    }

    // 根据ID取消车票（带事务）
    public boolean cancelTicket(Connection conn, int ticketId) {
        String sql = "UPDATE tickets SET 是否已取消 = true WHERE id = ?";
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, ticketId);
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 获取车票信息
    public Ticket getById(int ticketId) {
        String sql = "SELECT * FROM tickets WHERE id = ?";
        try (Connection conn = BaseDao.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, ticketId);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    return mapTicket(rs);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 从数据库中获取所有车票信息
    public List<Ticket> getAllTickets() {
        List<Ticket> tickets = new ArrayList<>();
        String sql = "SELECT * FROM tickets";
        try (Connection conn = BaseDao.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql);
             ResultSet rs = pstmt.executeQuery()) {
            while (rs.next()) {
                Ticket ticket = new Ticket();
                ticket.setCancelled(rs.getBoolean("是否已取消"));
                tickets.add(mapTicket(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return tickets;
    }

    // 将结果集映射为车票对象
    private Ticket mapTicket(ResultSet rs) throws SQLException {
        Ticket ticket = new Ticket();
        ticket.setId(rs.getInt("id"));
        ticket.setUserId(rs.getInt("用户ID"));
        ticket.setTrainId(rs.getInt("列车ID"));
        ticket.setSeatNumber(rs.getString("座位号"));
        ticket.setBookingTime(rs.getTimestamp("订票时间"));
        ticket.setPrice(rs.getDouble("票价"));
        ticket.setCancelled(rs.getBoolean("是否已取消"));
        ticket.setStudentTicket(rs.getBoolean("是否学生票"));

        return ticket;
    }
}